package com.mmp.main.servlet.report;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

/**
 * Servlet implementation class ViewJobOrdersClosed
 */
public class ViewJobOrdersClosed extends BasicServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		int year = WebUtil.getParamValueAsInteger(request, "year", 0);
		int month = WebUtil.getParamValueAsInteger(request, "month", 0);

		Map<Integer, Object> map = new HashMap<Integer, Object>();
		int i = 0;
		String jpql = "select j from JobOrder j where j.closed = true  ";

		if (month != 0) {
			jpql += " and MONTH(j.jobOrderDate) = ? ";
			map.put(++i, month);
		}

		if (month != 0) {
			jpql += " and YEAR(j.jobOrderDate) = ? ";
			map.put(++i, year);
		}

		jpql += " order by j.jobOrderDate";

		List<JobOrder> jOrderList = dbm.queryJPQL(jpql, map, JobOrder.class);
		if (jOrderList == null || jOrderList.isEmpty()) {
			announceErorrMessage(request, response, "لا يوجد أوامر عمل مغلقة للشهر والسنة التي تم اختيارها");
			return;
		}

		HashMap params = new HashMap();
		params.put(Default.REPORT_TITLE_PARAM_NAME, Enums.REPORT_TITLE.JOB_ORDER_CLOSED.getReportTitle());
		params.put("jobOrderList", jOrderList);
		viewPDFReport("rt12.jasper", null, params, response);
	}

}
